//
//  OderDetailController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/21.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import PKHUD

let kOrderDetailCellID = "kOrderDetailCellID";

class OrderDetailController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var orderId : String?
    var detailModel : OrderDetailModel?
    
    lazy var tableView : UITableView = {
        let table = UITableView.init(frame: self.view.bounds);
        
        table.backgroundColor = ZMColor.Gray.gray1;
        table.delegate = self;
        table.dataSource = self;
        table.registerNib("OrderDetailCell", kOrderDetailCellID);
        table.showsVerticalScrollIndicator = false;
        table.separatorStyle = .none;
        return table;
    }();
    
    lazy var receiveBtn: UIButton = {
        let button = UIButton.init();
        button.setTitle("收款成功", for: .normal);
        button.setTitleColor(.white, for: .normal);
        button.backgroundColor = ZMColor.Orange.orange1;
        button.layer.cornerRadius = 10;
        button.layer.masksToBounds = true;
        button.addTarget(self, action: #selector(receiveAction(button:)), for: .touchUpInside);
        return button;
    }()
    
    lazy var dataArray : [[String : String]] = {
        var array = Array<[String : String]>();
        
        let temp = [
            [
                "type" : "0",
                "title" : "订单号"
            ],
            [
                "type" : "1",
                "title" : "收款类型"
            ],
            [
                "type" : "2",
                "title" : "收款金额"
            ],
            [
                "type" : "3",
                "title" : "提交时间"
            ],
            [
                "type" : "4",
                "title" : "订单状态"
            ],
        ]
        array.append(contentsOf: temp);
        return array;
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
        self.orderDetailRequest();
        
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    // MARK: 👉🏻 🐷System Delegate🐷
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCell(withIdentifier: kOrderDetailCellID) as! OrderDetailCell;
        let dict = self.dataArray[indexPath.row];
        let type = dict["type"];
        switch type?.int() {
        case 0:
            cell.detailLabel.text = self.detailModel?.orderId;
            break;
        case 1:
            cell.detailLabel.text = self.detailModel?.typeDesc();
            break;
        case 2:
            cell.detailLabel.text = self.detailModel?.orderPrice;

            break;
        case 3:
            cell.detailLabel.text = self.detailModel?.commitTime;

            break;
        case 4:
            cell.detailLabel.text = self.detailModel?.statusDesc();

            break;
            
        default: break
            
            
        }
        cell.titleLabel.text = dict["title"];
        
        return cell;
    }
    // MARK: 👉🏻 🐷Custom Delegate🐷
    // MARK: 👉🏻 🐷Event  Response🐷
    @objc func receiveAction(button : UIButton){
        HUD.flash(.label("收款成功"), delay: 1);
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.navigationController?.popViewController(animated: true);
        }
        
    }
    // MARK: 👉🏻 🐷Private Methods🐷
    func orderDetailRequest()  {
        weak var weakSelf = self;
        let param = ["id" : self.orderId ?? ""];
        
        MoyaRequest(.receiveOrder(param), success: { (response : BaseResponse<OrderDetailModel>) in
            if response.respCode == 200{
                weakSelf?.detailModel = response.data;
                weakSelf?.tableView.reloadData();
            }else{
                HUD.flash(.label(response.respMessage),delay:1 );
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.navigationController?.popViewController(animated: true);
                }
            }
        }) { (error) in
            
        }
    }
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        self.view.backgroundColor = ZMColor.Gray.gray1;
        self.navigationItem.title = "订单详情";
        
    }
    func initSubViews()  {
        self.view.addSubview(self.tableView);
        self.view.addSubview(self.receiveBtn);
        
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview();
        };
        
        self.receiveBtn.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(20);
            make.trailing.equalToSuperview().offset(-20);
            make.height.equalTo(50);
            make.bottom.equalToSuperview().offset(-SafeBottom_H-25);
        };
    }
    
}
